package com.u2ds.dss.tools;

import org.apache.shiro.SecurityUtils;

import java.util.Map;

/**
 * Created by Ethan.Yuan on 2017/2/27.
 */
public class SessionTool {
    public static String getWeichat(){
        Object weichat = null;
        try{
            weichat = SecurityUtils.getSubject().getSession().getAttribute("openid");
        }catch (Exception e){
            //do nothing
        }
        return String.valueOf(weichat);
    }

    public static void setAttribute(String name, Object attr){
        SecurityUtils.getSubject().getSession().setAttribute(name, attr);
    }

    public static void setAttribute(Map<String, Object> attrMap){
        for(Map.Entry<String, Object> entry : attrMap.entrySet()){
            SecurityUtils.getSubject().getSession().setAttribute(entry.getKey(), entry.getValue());
        }
    }

    public static Object getAttribute(String name){
        return SecurityUtils.getSubject().getSession().getAttribute(name);
    }

    public static String getId(){
        Object id = getAttribute("id");
        return id == null ? null : id.toString();
    }


    public static String getUserName(){
        Object username = getAttribute("username");
        return username == null ? null : username.toString();
    }

    public static String getRole(){
        Object role = getAttribute("role");
        return role == null ? null : role.toString();
    }
}
